package com.tianle.tlapp.controller;


import com.tianle.tlapp.common.R;
import com.tianle.tlapp.entity.BalanceLog;
import com.tianle.tlapp.service.AppUserService;
import com.tianle.tlapp.service.BalanceLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Tianle
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/tlapp/app-user")
@Api(tags="api示例")
public class AppUserController {

    @Autowired
    private AppUserService userService;

    @Autowired
    private BalanceLogService logService;


    @GetMapping("getUserBalance/{id}")
    @ApiOperation("获取用户余额")
    public R getUserBalance(@PathVariable String id){

        BigDecimal userBalance = userService.getUserBalanceById(id);

        return R.ok().message("获取成功").data("userBalance",userBalance);
    }

    @PutMapping("consume/{id}/{number}")
    @ApiOperation("用户消费余额")
    public R consume(@PathVariable String id, @PathVariable BigDecimal number){

        //用户消费
        userService.userConsume(id,number);

        return R.ok().message("消费成功");
    }

    @PutMapping("refund/{id}/{number}")
    @ApiOperation("用户退款")
    public R refund(@PathVariable String id, @PathVariable BigDecimal number){

        //用户消费
        userService.userRefund(id,number);

        return R.ok().message("退款成功");
    }

    @GetMapping("getBalanceLog/{id}")
    @ApiOperation("获取用户金额变动明细")
    public R getBalanceLog(@PathVariable String id){

        //根据id获取用户余额日志
        List<BalanceLog> list = userService.getBalanceLogById(id);

        return R.ok().message("获取成功").data("logList",list);

    }

}

